import java.util.Arrays;

import static java.lang.System.exit;

public class StackList {
    //自己实现栈
    //先进后出
    //数组实现
    //记录数组的长度
    int size;
    int copySize;
    //创建一个数组
    int[]array;
    public StackList(int copySize) {
        this.copySize = copySize;
        array=new int[copySize];
        size=-1;
    }
    public StackList() {
        array=new int[4];
        this.size=4;
        size=-1;
    }
    //尾插
    public void push(int x) {
        if(copySize-1==size) {
            int newCopySize=copySize<4?4:2*copySize;
           array= Arrays.copyOf(array,newCopySize);
        copySize=newCopySize;
        }
        array[++size]=x;
    }
    //尾删
    public int pop() {
        //判断是否为空
        if(empty()) {
            System.out.println("栈为空");
            exit(0);
        }
        return array[size--];
    }
    //返回栈顶元素
    public int top() {
        if(empty()) {
            exit(0);
        }
        return array[size];
    }
    public boolean empty() {
        return size<0;
    }
    public void display() {
        while (!empty()) {
            System.out.print(pop()+" ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        StackList stackList=new StackList();
        stackList.push(1);
        stackList.push(2);
        stackList.display();
        System.out.println("a");

        stackList.pop();


    }
}
